package com.qingcloud.adminbackend.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.qingcloud.adminbackend.exception.WmCommonException;
import com.qingcloud.adminbackend.base.ApiTreeNode;
import com.qingcloud.adminbackend.entity.product.SysProductClassifyCommand;
import com.qingcloud.adminbackend.entity.product.SysProductCommandClassify;
import com.qingcloud.adminbackend.enums.BizExceptionEnum;
import com.qingcloud.adminbackend.mapper.SysProductClassifyCommandMapper;
import com.qingcloud.adminbackend.mapper.SysProductCommandClassifyMapper;
import com.qingcloud.adminbackend.pojo.bo.sysproduct.ProductClassifyCommand;
import com.qingcloud.adminbackend.pojo.bo.sysproduct.ProductCommandBO;
import com.qingcloud.adminbackend.pojo.dto.sysproduct.AddSysProductCommandClassifyDTO;
import com.qingcloud.adminbackend.pojo.dto.sysproduct.UpdateSysProductCommandClassifyDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品指令分类 服务实现类
 * </p>
 *
 * @author yangfan
 * @since 2021-11-09
 */
@Service
public class SysProductCommandClassifyServiceImpl extends ServiceImpl<SysProductCommandClassifyMapper, SysProductCommandClassify> implements ISysProductCommandClassifyService {


    @Resource
    private SysProductClassifyCommandMapper sysProductClassifyCommandMapper;

    /**
     * 产品指令分类树结构
     *
     * @param productTypeId 产品型号id
     * @return 树结构集合
     */
    @Override
    public List<ApiTreeNode> productCommandClassifyTree(Integer productTypeId) {
        //查询产品型号指令分类
        List<ProductClassifyCommand> productClassifyCommands = getBaseMapper().selectProductCommandClassify(productTypeId);
        List<ApiTreeNode> nodes = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(productClassifyCommands)) {
            for (ProductClassifyCommand productClassifyCommand : productClassifyCommands) {
                ApiTreeNode node = new ApiTreeNode();
                node.setLabel(productClassifyCommand.getProductClassifyName());
                node.setId(productClassifyCommand.getProductCommandClassifyId() + "");
                List<ApiTreeNode> children = new ArrayList<>();
                List<ProductCommandBO> productCommandList = productClassifyCommand.getProductCommandList();
                if (CollectionUtil.isNotEmpty(productCommandList)) {
                    for (ProductCommandBO productCommandBO : productCommandList) {
                        ApiTreeNode childNode = new ApiTreeNode();
                        childNode.setLabel(productCommandBO.getProductCommandName());
                        childNode.setId(productCommandBO.getProductCommandId() + "");
                        childNode.setGroupId(node.getId());
                        children.add(childNode);
                    }
                }
                node.setChildren(children);
                nodes.add(node);
            }
        }
        return nodes;
    }

    /**
     * 新增指令分类
     *
     * @param addSysProductCommandClassifyDTO 新增指令分类请求DTO
     * @return 指令分类id
     */
    @Override
    public Integer addSysProductCommandClassify(AddSysProductCommandClassifyDTO addSysProductCommandClassifyDTO, String currentUserId) {
        SysProductCommandClassify entity = new SysProductCommandClassify();
        cn.hutool.core.bean.BeanUtil.copyProperties(addSysProductCommandClassifyDTO, entity);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateDt(now);
        entity.setModifyDt(now);
        entity.setCreateBy(currentUserId);
        entity.setModifyBy(currentUserId);
        save(entity);
        return entity.getId();
    }

    /**
     * 更新指令分类
     *
     * @param updateSysProductCommandClassifyDTO 更新指令分类请求DTO
     * @return 更新结果
     */
    @Override
    public Boolean updateSysProductCommandClassifyById(UpdateSysProductCommandClassifyDTO updateSysProductCommandClassifyDTO, String currentUserId) {
        SysProductCommandClassify entity = new SysProductCommandClassify();
        cn.hutool.core.bean.BeanUtil.copyProperties(updateSysProductCommandClassifyDTO, entity);
        LocalDateTime now = LocalDateTime.now();
        entity.setModifyDt(now);
        entity.setModifyBy(currentUserId);
        return updateById(entity);
    }

    /**
     * 删除指令分类
     *
     * @param idList 指令分类Id列表
     * @return 删除结果
     */
    @Override
    public Boolean deleteSysProductCommandClassifyList(List<Integer> idList) {
        if(CollectionUtil.isEmpty(idList)){
            throw new WmCommonException(BizExceptionEnum.PARA_ERROR, "产品分类id列表不能为空");
        }
        //如果该分类下存在指令，则不能删除
        for(Integer classifyId : idList){
            LambdaQueryWrapper<SysProductClassifyCommand> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(SysProductClassifyCommand::getProductCommandClassifyId,classifyId);
            Long selectCount = sysProductClassifyCommandMapper.selectCount(queryWrapper);
            if(selectCount>0){
                throw new WmCommonException(BizExceptionEnum.DELETE_ERROR, "不能删除分类，该分类下存在指令,指令id:"+classifyId);
            }
        }
        return removeByIds(idList);
    }
}
